home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* Here's a generic program that draws a black screen with a red cross
- * going from corner to corner. It uses all the generic features:
- * checkpoint files, backup files, the pull-down menu, the generic
- * confirmer, and a generic master gizmo.
- *
- * The commands that read, write, insert, etc., files do nothing except
- * set the internal name of the currently open file. The Touch command
- * in the Edit pull-down menu simulates changing the file within the
- * program.
- *
- * It also has a dummy write failure in the file writing routine that
- * fails every fifth time to simulate out-of-space errors, etc.
- *
- * All the application-specific stuff is in this file, except for an
- * entry in the file pulldown.c that adds an edit pulldown with a
- * "touch" command in it.
- *
- * The included code should compile under both C and C++.
- */
-
- #include "generic.h"
- #include "gizmo.h" /* OPTIONAL - gizmos */
- #include <stdio.h>
- #include <gl.h>
- #include <device.h>
- #include <getopt.h>
-
- void initgraphics(), mainloop(), drawworld();
-
- extern int gd;
-
- int main(int argc, char **argv)
- {
- int ch;
-
- while ((ch = getopt(argc, argv, "fv")) != -1)
- switch (ch) {
- case 'f': /* foreground */
- foreground_flag = 1;
- break;
- case 'v': /* view-only */
- VIEW_ONLY = view_only = 1;
- break;
- case '?':
- (void)fprintf(stderr, "usage: %s [-fv] [file]\n", argv[0]);
- if (entries.exit) entries.exit();
- exit(-1);
- }
- MAKE_BACKUPS = 1; /* OPTIONAL - backups */
- if (foreground_flag) foreground();
- initgraphics();
- makepulldown(); /* OPTIONAL - pulldown menu */
- gd = (int)qgetfd(); /* OPTIONAL - gizmos */
- qdevice(WINQUIT); /* OPTIONAL - these calls catch
- qdevice(WINSHUT); * kills from the window manager */
- qdevice(KEYBD); /* if you want the alt-key stuff */
- qdevice(LEFTMOUSE);
- qdevice(MOUSEX); /* for locate-highlight in */
- qdevice(MOUSEY); /* the showcase libui. */
- glcompat(GLC_MQUEUERATE, GLC_COMPATRATE); /* ditto this line */
- if (argc - optind >= 1)
- doopen(argv[optind]);
- else if (VIEW_ONLY) {
- (void)fprintf(stderr, "You must name a file in view-only mode\n");
- if (entries.exit) entries.exit();
- exit(-1);
- }
- initsignal(); /* OPTIONAL */
- opengizmo(GIZMO_MASTER); /* OPTIONAL */
- makewintitle();
- mainloop();
- return 1; /* for lint's sake */
- }
-
- /* Initialize the graphics for the application. This generic application
- * draws a red X across a black background. If you use the generic pull-
- * down menu, remember that your editing area is reduced by PULLDOWNHEIGHT
- * in the y-direction.
- */
-
- void initgraphics()
- {
- minsize(400, 200);
- winopen(PROG_NAME);
- getwindowinfo();
- viewport(0, (short)(xsize - 1), 0, (short)(ysize - PULLDOWNHEIGHT));
- ortho2(-1.0, 1.0, -1.0, 1.0);
- }
-
- void handleredraw()
- {
- getwindowinfo();
- viewport(0, (short)(xsize - 1), 0, (short)(ysize - PULLDOWNHEIGHT));
- ortho2(-1.0, 1.0, -1.0, 1.0);
- remakepulldown(); /* required if you're using the pulldown */
- drawworld(); drawworld();
- }
-
- /* Your code should handle most of the events below except the last
- * one. It is a dummy command from the generic gizmo that touches
- * the file.
- */
-
- void dispatchcmd(long cmd)
- {
- switch (cmd) {
- case NEWCMD:
- handlenewcmd();
- break;
- case OPENCMD:
- handleopencmd();
- break;
- case SAVECMD:
- handlesavecmd();
- break;
- case SAVEASCMD:
- handlesaveascmd();
- break;
- case INSERTCMD:
- handleinsertcmd();
- break;
- case PRINTCMD:
- handleprintcmd();
- break;
- case QUITCMD:
- handlequitcmd();
- break;
- case HELPCMD:
- handlehelpcmd();
- break;
- case TOUCHME:
- if (view_only) {
- (void)message("View only file", 0, 0, 0);
- } else {
- dirtyfile_flag = 1;
- makewintitle();
- }
- break;
- }
- }
-
- void drawworld()
- {
- color(BLACK); clear();
- color(RED); move2(-1.0, -1.0); draw2(1.0, 1.0);
- move2(1.0, -1.0); draw2(-1.0, 1.0);
- drawpulldown();
- swapbuffers();
- }
-
- /* mainloop() is the main loop of the program that collects commands
- * and dispatches to the correct routines. In this application, commands
- * come from the pull-down menu only.
- */
-
- void mainloop()
- {
- long cmd = 0;
- long val, dev;
- static long ckpcount = 0;
-
- drawworld();
- while (1) {
- switch (dev = cookedqread(&val)) {
- case LEFTMOUSE:
- if (val == 0) break;
- cmd = pulldownevent(getvaluator(MOUSEX)-xorg, getvaluator(MOUSEY)-yorg);
- /* cmd == -1 if no event, or if the leftmouse is
- * outside the pulldown menu.
- */
- break;
- case WINQUIT:
- case WINSHUT:
- handlequitcmd();
- break;
- case REDRAW:
- handleredraw();
- break;
- case MOUSEX:
- case MOUSEY:
- /* possibly replace this with locate-highlight code */
- cmd = 0;
- break;
- case 0:
- ckpcount--;
- break;
- default:
- cmd = dev;
- break;
- }
- if (cmd) {
- dispatchcmd(cmd);
- drawworld();
- cmd = 0;
- if (ckpcount++ == 8) { /* Write a checkpoint file every 8 events */
- writecheckpointfile();
- ckpcount = 0;
- }
- }
- }
- }
-
- long readfile(char *);
- long writefile(char *);
- long insertfile(char *);
- long errorwritefile(char *);
- long printfile(char *);
- void makecheckpointname(char *);
- void makebackupname(char *);
- void clearbuffer();
- void generichelp();
- void quit();
- void appexit(); /* for errors */
-
- /* You've got to fill in slots in this structure with the application-
- * specific routine names. If you don't have a routine, just put zero
- * in the slot. You had better have a readfile and writefile, at least.
- *
- * readfile() takes the file name, reads the contents into your
- * internal data structures, and returns 1 if successful; 0 otherwise.
- *
- * writefile() writes a file with the given name, and returns 1 if
- * successful; zero otherwise.
- *
- * errorwritefile() writes the file after a signal is caught. This
- * routine can be the same as writefile(), but you may want something
- * more robust or different, since when this routine gets hit, there
- * was probably a bad error.
- *
- * insertfile() adds the contents of the named file to the application's
- * internal data structures.
- *
- * printfile() prints. The application creates a print file with the
- * given name, and then issues an 'lp' command, or whatever's appropriate.
- *
- * makecheckpointname() creates the name of the checkpoint files. By
- * default, the file named 'foo' has a checkpoint name of 'foo.ckp'. You
- * may want to strip extensions, etc.
- *
- * makebackupname() same as above, but for backup files. The default
- * backup name for 'foo' is 'foo.bak'.
- *
- * clearbuffer() clears the application's data structures after something
- * like a New command.
- *
- * help() presents help (or indicates that none is available).
- *
- * quit() cleans up stuff before quitting.
- */
-
- struct application_entries entries = {
- readfile,
- writefile,
- errorwritefile,
- insertfile,
- printfile,
- makecheckpointname,
- makebackupname,
- clearbuffer,
- generichelp,
- quit,
- appexit
- };
-
- void appexit()
- {
- (void)fprintf(stderr, "Exit\n");
- exit(0);
- }
-
- void generichelp()
- {
- (void)fprintf(stderr, "No help available\n");
- }
-
- void clearbuffer()
- {
- (void)fprintf(stderr, "Cleared the buffer\n");
- }
-
- long readfile(char *filename)
- {
- (void)fprintf(stderr, "Read file: %s\n", filename);
- return 1;
- }
-
- void quit()
- {
- murdergizmos(); /* OPTIONAL */
- }
-
- /*
- * writefile() generates an "error" every fifth time to test
- * the code that handles write errors.
- */
-
- long writefile(char *filename)
- {
- static long i = 0;
- if (i == 4) {
- (void)fprintf(stderr, "Write error\n");
- i = 0;
- return 0;
- }
- i++;
- (void)fprintf(stderr, "Wrote file: %s\n", filename);
- return 1;
- }
-
- long errorwritefile(char *filename)
- {
- (void)fprintf(stderr, "Caught signal: ");
- return writefile(filename);
- }
-
- long insertfile(char *filename)
- {
- (void)fprintf(stderr, "Inserted file: %s\n", filename);
- dirtyfile_flag = 1;
- return 1;
- }
-
- long printfile(char *filename)
- {
- (void)fprintf(stderr, "Printed file: %s\n", filename);
- return 1;
- }
-
- void makecheckpointname(char *filename)
- {
- (void)sprintf(filename, "%s.CKP", currentfilename);
- }
-
- void makebackupname(char *filename)
- {
- (void)sprintf(filename, "%s.BAK", currentfilename);
- }
-
-